﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Drawing;

namespace TJBHJJ.Models
{
    public static class FileUpload
    {
        //public static char DirSeparator = Path.DirectorySeparatorChar;
        //public static string FilesPath = HttpContext.Current.Server.MapPath(string.Format("Content{0}Uploads{1}", DirSeparator, DirSeparator)) + "/Content/Files/";
        public static string FilesPath = HttpContext.Current.Server.MapPath("/");
        static string partpath = FilesPath + "Content\\Files\\";
        public static string UploadFile(HttpPostedFileBase file)
        {
            // Check if we have a file            
            if (null == file) return "";
            // Make sure the file has content            
            if (!(file.ContentLength > 0)) return "";
            string fileExt = Path.GetExtension(file.FileName);
            string fileName =  file.FileName;
            // Make sure we were able to determine a proper            
            // extension            
            if (null == fileExt) return "";
            // Check if the directory we are saving to exists            
            //if (!Directory.Exists(Path.GetFullPath(partpath).Substring(3)))
            //{
            //    // If it doesn't exist, create the directory                
            //    Directory.CreateDirectory(Path.GetFullPath(partpath).Substring(3));
            //}
            if (!Directory.Exists(Path.GetFullPath(partpath)))
            {
                // If it doesn't exist, create the directory                
                Directory.CreateDirectory(Path.GetFullPath(partpath));
            }
            // Set our full path for saving            
            //string path = FilesPath + DirSeparator + fileName;
            string path = partpath + fileName;
            // Save our file            
            file.SaveAs(Path.GetFullPath(path));
            // Return the filename     
       
            //return "Content/Files/" +fileName;
            return fileName;
        }
        public static void DeleteFile(string fileName)
        {
            // Don't do anything if there is no name            
            if (fileName.Length == 0) return;
            // Set our full path for deleting            
            //string path = FilesPath + DirSeparator + fileName;
            //string path = FilesPath + fileName;
            //string path = Path.GetFullPath(FilesPath) + Path.GetFullPath(fileName).Substring(3);
            string path = Path.GetFullPath(FilesPath + fileName);
            // Check if our file exists            
            if (File.Exists(path))
            {
                // Delete our file                
                File.Delete(path);
            }
        }
    }

    public static class ThumbnailImage
    {
        public static bool ThumbnailCallback()
        {
            return false;
        }
        public static string GetThumb(string filename, int x, int y)
        {
            if (filename == null || filename == "")
                return "/Images/image_51x51.gif";

            string partpath = "/Content/Files/";
            string name = FileUpload.FilesPath + partpath + filename;
            name = Path.GetFullPath(name);

            if (!File.Exists(name))
            {
                return "/Images/image_51x51.gif";
            }
            filename = partpath + filename.Insert(filename.IndexOf('.'), "_" + x.ToString() + "_" + y.ToString());
            if (File.Exists(Path.GetFullPath(FileUpload.FilesPath + filename)))
            {
                return filename;
            }
            Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
            Bitmap myBitmap = new Bitmap(name);
            Image myThumbnail = myBitmap.GetThumbnailImage(x, y, myCallback, IntPtr.Zero);
            if (!File.Exists(Path.GetFullPath(FileUpload.FilesPath + filename)))
            {
                myThumbnail.Save(Path.GetFullPath(FileUpload.FilesPath + filename));
            }
            return filename;
        }

    }
}
